package control;

import javax.microedition.lcdui.Graphics;

import main.Res;

public class DrawButton extends Control{
	private String text;
	int x,y,w,h;
	int index;
	protected boolean isToggled= false;
	public boolean isToggled() {
		return isToggled;
	}
	public void setToggled(boolean isToggled) {
		this.isToggled = isToggled;
	}
	public void toggled() {
		isToggled = !isToggled;
	}
	boolean isHover = false;
	public DrawButton(int iconIndex,
			int x,int y, int w, int h) {
		init(iconIndex,x,y, w, h,"");
	}
	public DrawButton(int iconIndex,
			int x,int y, int w, int h,String text) {
		init(iconIndex,x,y, w, h,text);
	}
	public void setIcon(int i){
		index = i;
	}
	private void init(int iconIndex,
			int x,int y, int w, int h,String text)
	{
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		index = iconIndex;
		this.setText(text);
	}
	protected boolean isHover(int pX,int pY)
	{
		return (pX>x
				&&pY>y
				&&pX<x+w
				&&pY<y+h);
	}
	public boolean pointerPressed(int x,int y)
	{
		isHover = isHover(x,y);
		return isHover;
	}
	
	public boolean pointerReleased(int x,int y)
	{
		isHover = false;
		
		if(isHover(x,y))
		{
			if(command!= null)
			{
				Res.PlaySound(4);
				command.Do();
			}
			return true;
		}
		return false;
	}
	
	protected void controlPaint(Graphics g)
	{
		if(isHover || isToggled)
		{
			g.setColor(COLOR_PRESSED);
		}
		else
		{
			g.setColor(COLOR_BACK);
		}
		g.fillRect(x, y, w, h);
		g.setColor(COLOR_FORE);
		
		/*g.drawRect(x, y, w, h);
		g.drawRect(x-1, y-1, w+2, h+2);
		g.drawRect(x+1, y+1, w-2, h-2);*/
		Res.drawIcon(g, index, x+(w-Res.ICON_SIZE)/2, y+(h-Res.ICON_SIZE)/2);
		
		g.setColor(COLOR_PRESSED);
		
		g.setFont(font);
		g.drawString(getText(), x+w/2+1, y+h-5,
				Graphics.BOTTOM | Graphics.HCENTER);
		g.setColor(COLOR_FORE);
		g.drawString(getText(), x+w/2, y+h-4,
				Graphics.BOTTOM | Graphics.HCENTER);
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
}
